$(function () {
    obj.init();
});
var obj = new function () {
    'use strict';
    return {
        init: function () {
            this.listener();
        },
        listener: function () {
            var $this = this;
            this.funObj().getCharts();
            $('.export-data').click(function () {
                location.href = baseUrl + 'Article/exportStat';
            });

            $("#bt_list_search").click(function () {
                var startTime1  = xhExtra.getValue($('#list_search_startTime_one'));
                var startTime2  = xhExtra.getValue($('#list_search_startTime_two'));
                var endTime1  = xhExtra.getValue($('#list_search_endTime_one'));
                var endTime2  = xhExtra.getValue($('#list_search_endTime_two'));
                $.post('/Article/stat', {startTime1: startTime1, startTime2: startTime2, endTime1: endTime1, endTime2: endTime2}, function (data) {
                    var html = '';
                    for (let index in data.data) {
                        var subject = data.data[index].subject;
                        var number = data.data[index].number;
                        html += "<tr><td>"+subject+"</td><td>"+number+"</td></tr>";
                    }
                    document.getElementById('statData').innerHTML = '';
                    $("#statData").append(html);
                    $("#total").html(data.total);

                    document.getElementById('stat').innerHTML = '';
                    var myChart = echarts.init(document.getElementById('stat'));

                    var labelOption = {
                        normal: {
                            formatter: '{c}',
                            rich: {
                                name: {
                                    textBorderColor: '#fff'
                                }
                            },
                            position: 'top',
                            show: true,
                            distance: 8,
                            align: 'center',
                            verticalAlign: 'bottom',
                        }
                    };

                    var option = {
                        title: {
                            text: '活动统计'
                        },
                        tooltip: {},
                        legend: {
                            data:['数量']
                        },
                        xAxis: {
                            data: data.xList
                        },
                        yAxis: {},
                        series: [{
                            name: '数量',
                            type: 'bar',
                            data: data.yList,
                            label: labelOption
                        }]
                    };
                    myChart.setOption(option);
                });

            })
        },
        funObj: function () {
            var $this = this;
            return {
                getCharts: function () {
                    var myChart = echarts.init(document.getElementById('stat'));
                    myChart.setOption({
                        title: {
                            text: '活动统计'
                        },
                        tooltip: {},
                        legend: {
                            data:['数量']
                        },
                        xAxis: {
                            data: []
                        },
                        yAxis: {},
                        series: [{
                            name: '数量',
                            type: 'bar',
                            data: []
                        }]
                    });

                    var labelOption = {
                        normal: {
                            formatter: '{c}',
                            rich: {
                                name: {
                                    textBorderColor: '#fff'
                                }
                            },
                            position: 'top',
                            show: true,
                            distance: 8,
                            align: 'center',
                            verticalAlign: 'bottom',
                        }
                    };

                    $.get(baseUrl + 'Article/stat').done(function (data) {
                        var html = '';
                        for (let index in data.data) {
                            var subject = data.data[index].subject;
                            var number = data.data[index].number;
                            html += "<tr><td>"+subject+"</td><td>"+number+"</td></tr>";
                        }
                        $("#statData").append(html);
                        $("#total").html(data.total);

                        myChart.setOption({
                            xAxis: {
                                data: data.xList
                            },
                            series: [{
                                name: '数量',
                                data: data.yList,
                                label: labelOption
                            }]
                        });

                    });

                }
            }
        }
    }
}
